﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DarkCRM.Helpers;
using System.Data;

namespace DarkCRM.Models
{
    public class ControllerAction:Entity
    {
        public int ActionID { get; set; }
        public string ActionName { get; set; }
        public string ClassName { get; set; }
        public string FunctionName { get; set; }
        public bool IsDeleted { get; set; }
        public bool IsLock { get; set; }
        public string ActionDescription { get; set; }
        public int ParentID { get; set; }

        public ControllerAction()
        {
        
        }


        /// <summary>
        /// 检查函数是否允许执行
        /// </summary>
        /// <param name="memberid"></param>
        /// <param name="className"></param>
        /// <param name="functionName"></param>
        /// <returns></returns>
        public static bool CheckAllowToAction(int memberid,int roleid,string className,string functionName)
        {
            IDictionary<string, object> par = new Dictionary<string, object>();
            par.Add("MemberID", memberid);
            par.Add("RoleID", roleid);
            par.Add("ClassName", className);
            par.Add("FunctionName", functionName);
            return SqlHelper.ExecuteScalar("CheckAllowAction", par).ToString() == "1";
        }

        public static IList<ControllerAction> GetAllControllerAction()
        {
            DataTable dt= SqlHelper.ExecuteReader("GetAllControllAction").Tables[0];
            EntityList<ControllerAction> list = new EntityList<ControllerAction>();
            list.Load(dt);
            return list;
        }

        public static bool CreateAction(ControllerAction action)
        {
            IDictionary<string, object> par = new Dictionary<string, object>();
            par.Add("ActionName", action.ActionName);
            par.Add("ClassName", action.ClassName);
            par.Add("FunctionName",action.FunctionName);
            par.Add("ActionDescription",action.ActionDescription);
            par.Add("ParentID",action.ParentID);
            int i = SqlHelper.ExecuteNonQuery("CreateControllerAction", par);
            return i > 0;
        }

        #region for ajax
        /// <summary>
        /// 获取controllAction ID和Name， 绑定dropdwon
        /// </summary>
        /// <returns></returns>
        [DarkAjax.Utility.jQueryInvokeMethod(Url = "handlers/AjaxController.ashx", Method = "get", DataType = "json", Async = false)]
        public static ResultMessage GetControllerActionName()
        {
            IList<ControllerAction> models = GetAllControllerAction();
            models.Insert(0, new ControllerAction() { ActionID = 0, ActionName = "无" });
            var ps = models.Select(m => new { value = m.ActionID, text = m.ActionName });
            return new ResultMessage() { ErrorCode = 0, Lable = "success", Data = ps };
        }

        /// <summary>
        /// 创建ControllerAction
        /// </summary>
        /// <param name="custom"></param>
        /// <returns></returns>
        [DarkAjax.Utility.jQueryInvokeMethod(Url = "handlers/AjaxController.ashx", Method = "post", DataType = "json", Async = true)]
        public static ResultMessage CreateControllerAction(ControllerAction action)
        {
            bool success = ControllerAction.CreateAction(action);
            if (success)
            {
                return new ResultMessage() { ErrorCode = 0, Lable = "success", Value = success, Data = action };
            }
            return new ResultMessage() { ErrorCode = 1001, Lable = "faile",Description="添加失败，请检查是否重复数据。", Value=success, Data = action };
        }
        #endregion
    }
}
